package org.atlantis.mongo;

import java.util.HashMap;
import java.util.Map;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;

public class MongoFactory {
	private static final String DB_NAME = "trialdb";
	private static DB db;
	private static final Map<String, DBCollection> collCache = new HashMap<String, DBCollection>();

	static {
		try {
			init();
		} catch (Exception e) {
		}
	}

	private static void init() throws Exception {
		Mongo mongo = new Mongo();
		db = mongo.getDB(DB_NAME);
		for (String collName : db.getCollectionNames()) {
			collCache.put(collName, db.getCollectionFromString(collName));
		}
	}

	public static DB getDB() {
		return db;
	}
	
	public static String getDBName() {
		return DB_NAME;
	}

	public static DBCollection getCollection(String collName) {
		DBCollection coll = collCache.get(collName);
		if (coll != null) return coll;
		coll = db.getCollection(collName);
		collCache.put(collName, coll);
		return coll;
	}
}
